home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / sendmail / sendmail-5.65c+IDA-1.4.4.1 / src / setenv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-24  |  3.9 KB  |  151 lines

  1. /*
  2.  * Copyright (c) 1987 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that the above copyright notice and this paragraph are
  7.  * duplicated in all such forms and that any documentation,
  8.  * advertising materials, and other materials related to such
  9.  * distribution and use acknowledge that the software was developed
  10.  * by the University of California, Berkeley.  The name of the
  11.  * University may not be used to endorse or promote products derived
  12.  * from this software without specific prior written permission.
  13.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  14.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  15.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  16.  */
  17.  
  18. #include "sendmail.h"
  19.  
  20. #if defined(ISC)
  21. # include <sys/bsdtypes.h>
  22. #endif /* ISC */
  23.  
  24. #if defined(NEED_GETENV) || defined(NEED_SETENV) || defined(NEED_UNSETENV)
  25.  
  26. # if !defined(lint)
  27. static char sccsid[] = "@(#)setenv.c    5.2 (Berkeley) 6/27/88";
  28. static char rcsid[] = "@(#)$Id: setenv.c,v 5.2.0.4 1991/06/24 20:19:37 paul Exp $";
  29. # endif /* not lint */
  30.  
  31. /*
  32.  * _findenv --
  33.  *    Returns pointer to value associated with name, if any, else NULL.
  34.  *    Sets offset to be the offset of the name/value combination in the
  35.  *    environmental array, for use by setenv(3) and unsetenv(3).
  36.  *    Explicitly removes '=' in argument name.
  37.  */
  38. static char *
  39. _findenv(name, offset)
  40.     register char *name;
  41.     int *offset;
  42. {
  43.     extern char **environ;
  44.     register int len;
  45.     register char **P, *C;
  46.  
  47.     for (C = name, len = 0; *C && *C != '='; ++C, ++len);
  48.     for (P = environ; *P; ++P)
  49.         if (!strncmp(*P, name, len))
  50.             if (*(C = *P + len) == '=') {
  51.                 *offset = P - environ;
  52.                 return(++C);
  53.             }
  54.     return(NULL);
  55. }
  56. #endif /* NEED_GETENV || NEED_SETENV || NEED_UNSETENV */
  57.  
  58. #ifdef NEED_SETENV
  59. /*
  60.  * setenv --
  61.  *    Set the value of the environmental variable "name" to be
  62.  *    "value".  If rewrite is set, replace any current value.
  63.  */
  64. setenv(name, value, rewrite)
  65.     register char *name, *value;
  66.     int rewrite;
  67. {
  68.     extern char **environ;
  69.     static int alloced;            /* if allocated space before */
  70.     register char *C;
  71.     int l_value, offset;
  72.  
  73.     if (*value == '=')            /* no `=' in value */
  74.         ++value;
  75.     l_value = strlen(value);
  76.     if ((C = _findenv(name, &offset))) {    /* find if already exists */
  77.         if (!rewrite)
  78.             return(0);
  79.         if (strlen(C) >= l_value) {    /* old larger; copy over */
  80.             while (*C++ = *value++);
  81.             return(0);
  82.         }
  83.     }
  84.     else {                    /* create new slot */
  85.         register int    cnt;
  86.         register char    **P;
  87.  
  88.         for (P = environ, cnt = 0; *P; ++P, ++cnt);
  89.         if (alloced) {            /* just increase size */
  90.             environ = (char **)realloc((char *)environ,
  91.                 (u_int)(sizeof(char *) * (cnt + 2)));
  92.             if (!environ)
  93.                 return(-1);
  94.         }
  95.         else {                /* get new space */
  96.             alloced = 1;        /* copy old entries into it */
  97.             P = (char **)malloc((u_int)(sizeof(char *) *
  98.                 (cnt + 2)));
  99.             if (!P)
  100.                 return(-1);
  101.             bcopy(environ, P, cnt * sizeof(char *));
  102.             environ = P;
  103.         }
  104.         environ[cnt + 1] = NULL;
  105.         offset = cnt;
  106.     }
  107.     for (C = name; *C && *C != '='; ++C);    /* no `=' in name */
  108.     if (!(environ[offset] =            /* name + `=' + value */
  109.         malloc((u_int)((int)(C - name) + l_value + 2))))
  110.         return(-1);
  111.     for (C = environ[offset]; (*C = *name++) && *C != '='; ++C);
  112.     for (*C++ = '='; *C++ = *value++;);
  113.     return(0);
  114. }
  115. #endif /* NEED_SETENV */
  116.  
  117. #ifdef NEED_UNSETENV
  118. /*
  119.  * unsetenv(name) --
  120.  *    Delete environmental variable "name".
  121.  */
  122. void
  123. unsetenv(name)
  124.     char    *name;
  125. {
  126.     extern    char    **environ;
  127.     register char    **P;
  128.     int    offset;
  129.  
  130.     while (_findenv(name, &offset))        /* if set multiple times */
  131.         for (P = &environ[offset];; ++P)
  132.             if (!(*P = *(P + 1)))
  133.                 break;
  134. }
  135. #endif /* NEED_UNSETENV */
  136.  
  137. #ifdef NEED_GETENV
  138. /*
  139.  * getenv --
  140.  *    Returns ptr to value associated with name, if any, else NULL.
  141.  */
  142. char *
  143. getenv(name)
  144.     char *name;
  145. {
  146.     int offset;
  147.  
  148.     return(_findenv(name, &offset));
  149. }
  150. #endif /* NEED_GETENV */
  151.